package crp.core.resource.app;

import crp.core.resource.domain.repositories.CourseResourceRepository;
import com.qwlabs.cdi.Dispatchable;
import crp.core.workflow.domain.services.WorkflowExternalInfoSearcher;

import javax.enterprise.context.ApplicationScoped;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@ApplicationScoped
public class ResourceWorkflowExternalInfoSearcher implements WorkflowExternalInfoSearcher, Dispatchable<String> {
    private final CourseResourceRepository courseResourceRepository;

    public ResourceWorkflowExternalInfoSearcher(CourseResourceRepository courseResourceRepository) {
        this.courseResourceRepository = courseResourceRepository;
    }

    @Override
    public Map<String, Object> externalInfo(String externalId, String topic) {
        Map<String, Object> objectMap = new HashMap<>();
        courseResourceRepository.findByResourceIds(Set.of(externalId)).findFirst().ifPresent(item -> {
            objectMap.put("resourceName", item.getResource().getName());
            objectMap.put("courseCode", item.getCourse().getCode());
            objectMap.put("courseName", item.getCourse().getName());
            objectMap.put("duration", item.getResource().getDuration());
        });
        return objectMap;
    }

    @Override
    public boolean dispatchable(String topic) {
        return "RESOURCE".equalsIgnoreCase(topic);
    }
}
